package cn.etynit.basic.util;

import cn.etynit.basic.jwt.*;
import cn.etynit.user.domain.Logininfo;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

@Slf4j
public class LoginContext {
    /**
     * 获取登录人信息的类
     * @param request
     * @return
     */
    public static Logininfo getLogininfo(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
             PublicKey publicKey = RsaUtils.getPublicKey(JwtRasHolder.INSTANCE.getJwtRsaPubData());
            Payload<UserInfo> payload = JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
            UserInfo userInfo = payload.getUserInfo();
            if (null == userInfo){
                log.error("用户未登录！");
                return null;
            }
            return userInfo.getLogininfo();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
